package com.ezcolour.ticket.web.controller;

import com.ezcolour.ticket.base.BaseController;
import com.ezcolour.ticket.base.BaseException;
import com.ezcolour.ticket.bean.JwtProperties;
import com.ezcolour.ticket.common.util.CrawlerUtil;
import com.ezcolour.ticket.model.VocalBean;
import com.ezcolour.ticket.service.VocalBeanService;
import com.ezcolour.ticket.support.BizExceptionEnum;
import com.ezcolour.ticket.util.JwtTokenUtil;
import com.ezcolour.ticket.web.global.GlobalCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 *  演唱会详情
 *
 * @author rhp
 * @date 2017-08-23 9:23
 */
@Controller
@RequestMapping("/web/api")
public class ProjectWebController extends BaseController {

    @Autowired
    private VocalBeanService vocalBeanService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     *
     */
    @RequestMapping(value = "/project", method = RequestMethod.GET)
    public ResponseEntity<?> getOrder(String projectId) {
        String info = CrawlerUtil.getInfo(projectId);
        return ResponseEntity.ok(info);
    }

    /**
     *
     */
    @RequestMapping(value = "/projects", method = RequestMethod.GET)
    public ResponseEntity<?> getPriceList(String projectId,String performId) {
        String list = CrawlerUtil.getPriceList(projectId,performId);
        return ResponseEntity.ok(list);
    }

    /**
     *
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ResponseEntity<?> search(String keyword) {
        String list = null ;
        try{
            list = CrawlerUtil.getProjectList(keyword);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  ResponseEntity.ok(list);
    }

    /**
     *
     */
    @RequestMapping(value = "/shop", method = RequestMethod.POST)
    public ResponseEntity<?> addOrder(HttpServletRequest request,VocalBean vocalBean) {
        String username = getLoginUserName(request);
        List<VocalBean> list = vocalBeanService.findByPriceId(username,vocalBean.getPriceId());
        VocalBean save;
        if(list!=null && list.size()>0){
            save = list.get(0);
            save.setTime(new Date());
            save.setNumber(vocalBean.getNumber());
            vocalBeanService.save(save);
        }else{
            String companyId = CrawlerUtil.getCompanyId(vocalBean.getProjectId());
            vocalBean.setCompanyId(companyId);
            vocalBean.setUsername(username);
            vocalBean.setTime(new Date());
            vocalBean.setStatus(1);
            save = vocalBeanService.save(vocalBean);
        }

        ConcurrentHashMap<Long,VocalBean> vocalMap = GlobalCache.user_vocal.get(username);
        if(vocalMap==null || vocalMap.size()<=0){
            vocalMap = new ConcurrentHashMap<>();
        }

        vocalMap.put(save.getId(),save);
        System.out.println("添加"+save.getId());
        GlobalCache.user_vocal.put(username,vocalMap);
        return  ResponseEntity.ok("success");
    }

    /**
     *
     */
    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    public ResponseEntity<?> findAllOrders(HttpServletRequest request) {
        String username = getLoginUserName(request);
        ConcurrentHashMap<Long, VocalBean> ordersMap = GlobalCache.me().getOrderByCache(username);
        if(ordersMap==null || ordersMap.size()<=0){
            throw new BaseException(BizExceptionEnum.AUTH_COOKIE_ERROR);
        }
        Iterator iter = ordersMap.entrySet().iterator();
        List<VocalBean> list = new ArrayList<VocalBean>();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            list.add((VocalBean)entry.getValue());
            System.out.println(((VocalBean)entry.getValue()).toString());
        }
        return  ResponseEntity.ok(list);
    }

    /**
     *
     */
    @RequestMapping(value = "/orders", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteOrder(HttpServletRequest request,Long orderId) {
        String username = getLoginUserName(request);
        VocalBean vocal = vocalBeanService.findById(orderId);
        if(!vocal.getUsername().equals(username)){
            throw  new BaseException(BizExceptionEnum.NO_PERMITION);
        }
        ConcurrentHashMap<Long, VocalBean> ordersMap = GlobalCache.user_vocal.get(username);
        ordersMap.remove(vocal.getId());
        vocalBeanService.delete(vocal);

        return  ResponseEntity.ok("success");
    }

    public String getLoginUserName(HttpServletRequest request){
        final String requestHeader = request.getHeader(jwtProperties.getHeader());
        String authToken = requestHeader.substring(7);
        return jwtTokenUtil.getUsernameFromToken(authToken);
    }


}
